import os
import zipfile

from classpath.entry import Entry


class ZipEntry(Entry):

    def __init__(self, absPath):
        self.absPath = absPath
        self.reader = None

    def readClass(self, className):
        if self.reader is None:
            self.reader = zipfile.ZipFile(self.absPath, "r")
        for name in self.reader.namelist():
            if os.path.basename(name) == className:
                with self.reader.open(name, "r") as f:
                    return f.read(), self
        return None, self

    def __str__(self):
        return self.absPath

    @staticmethod
    def newZipEntry(path):
        return ZipEntry(os.path.abspath(path))


if __name__ == '__main__':
    entry = ZipEntry.newZipEntry(r"C:\Users\admin\Downloads\jvmgo-book-master.zip")
    content, en = entry.readClass("test.sh")
    print(content)
